﻿using System.Data;
using Litografia.BusinessEntities;

namespace Litografia.DataObjects
{
    public sealed class DOParametrosBodega : DOBase
    {
        #region Declarations

        private const string GetSP = "parametrosBodegaGet";
        private const string UpdateSP = "parametrosBodegaUpdate";
        #endregion

        #region Methods: Public
        public static BEParametrosBodega Get(int empresaId, int sucursalId)
        {
            var command = DB.GetStoredProcCommand(GetSP);
            DB.AddInParameter(command, "@empresa_id", DbType.Int32, empresaId);
            DB.AddInParameter(command, "@sucursal_id", DbType.Int32, sucursalId);
            var dataset = DB.ExecuteDataSet(command);

            return ConvertRawToEntity(dataset);
        }

        public static void Update(BEParametrosBodega.ParametrosBodegaRow parametrosBodega)
        {
            var command = DB.GetStoredProcCommand(UpdateSP);

            DB.AddInParameter(command, "@empresa_id", DbType.Int32, parametrosBodega.EmpresaId);
            DB.AddInParameter(command, "@sucursal_id", DbType.Int32, parametrosBodega.SucursalId);
            DB.AddInParameter(command, "@bodega_materia_prima", DbType.Int32, parametrosBodega.BodegaMateriaPrima);
            DB.AddInParameter(command, "@bodega_producto_proceso", DbType.Int32, parametrosBodega.BodegaProductoEnProceso);
            DB.AddInParameter(command, "@bodega_producto_terminado", DbType.Int32, parametrosBodega.BodegaProductoTerminado);
            DB.ExecuteNonQuery(command);
        }
        #endregion

        #region Methods: Private
        private static BEParametrosBodega ConvertRawToEntity(DataSet rawDataSet)
        {
            var entity = new BEParametrosBodega {EnforceConstraints = false};

            for (var i = 1; i < rawDataSet.Tables.Count; i++)
                rawDataSet.Tables[i].TableName = rawDataSet.Tables[0].Rows[i]["TableName"].ToString();

            entity.Merge(rawDataSet);

            entity.EnforceConstraints = true;
            return entity;
        }
        #endregion
    }
}
